package Day36.equals;

import java.util.Comparator;
import java.util.Date;

public class User implements Comparable<User>{
    private Integer userId;
    private String username;
    private Integer userScore;
    private Date regDate;

    public User() {
    }

    public User(Integer userId, String username, Integer userScore, Date regDate) {
        this.userId = userId;
        this.username = username;
        this.userScore = userScore;
        this.regDate = regDate;
    }



    @Override
    public boolean equals(Object obj){
        System.out.println("执行User的equals方法...");
        if(obj==null){
            return false;
        }
        if(obj instanceof User){
            User user = (User)obj;
            return this.userId==user.getUserId() && this.username.equals(user.getUsername());
        }
        return false;
    }
    @Override
    public int hashCode(){
        System.out.println("执行User的hashCode方法...");
        return 1;
    }

    public User(Integer userId, String username) {
        this.userId = userId;
        this.username = username;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public int compareTo(User user) {

        return this.userId - user.getUserId();
    }

    public Integer getUserScore() {
        return userScore;
    }

    public void setUserScore(Integer userScore) {
        this.userScore = userScore;
    }

    public Date getRegDate() {
        return regDate;
    }

    public void setRegDate(Date regDate) {
        this.regDate = regDate;
    }

    @Override
    public String toString() {
        return "User{" +
                "userId=" + userId +
                ", username='" + username + '\'' +
                ", userScore=" + userScore +
                ", regDate=" + regDate +
                '}';
    }


/**
 * 使用内部类定义扩展规则
 * 1.根据积分升序排序
 */
    static class sortByScoreASC implements Comparator<User>{


    @Override
    public int compare(User user1, User user2) {
        return user1.getUserScore() - user2.getUserScore();
    }
}

/**
 * 2.根据积分降序排序
 */

static class sortByScoreDESC implements Comparator<User>{


    @Override
    public int compare(User user1, User user2) {
        return user2.getUserScore() - user1.getUserScore();
    }
}


    /**
     * 2.根据学号降序排序
     */

    static class sortByUserIdDESC implements Comparator<User>{


        @Override
        public int compare(User user1, User user2) {
            return user2.getUserId() - user1.getUserId();
        }
    }

}
